package com.technology.education.ui.teacher.deposit

import android.view.Gravity
import androidx.core.os.bundleOf
import com.angcyo.dsladapter.dpi
import com.angcyo.dsladapter.dslItem
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.SpKeys
import com.technology.education.data.model.UserInfoModel
import com.technology.education.dslitem.ItemSingleButton
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.*
import com.technology.education.ui.common.information.CompleteWithdrawActivity
import com.technology.education.widget.CommonButton
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.startActivity

/**
 *@author:create by BZY
 *Date: 2022/3/1 13
 * 保证金管理
 */
class DepositFragment : BaseRecyclerTopBarFragment<DepositViewModel>() {
    override fun initViews() {
        setTitle("保证金管理")
    }

    override fun lazyLoadData() {
        loading()
    }

    private fun setUI(userInfoModel: UserInfoModel) {
        renderAdapter {
            clearAllItems()
            dslItem(R.layout.item_deposit_info) {
                itemPaddingTop = 15 * dpi
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.tv(R.id.textview)?.text = "保证金金额"
                    itemHolder.tv(R.id.tv_deposit)?.text =
                        userInfoModel.depositMoney

                }
            }

            dslItem(
                ItemSingleButton(
                    titleDrawable =
                    if (userInfoModel.depositState == "0") R.drawable.bg_button else R.drawable.bg_white_round_blue_line
                )
            ) {
                style.topPadding = 44
                style.leftPadding = 0
                style.rightPadding = 0
                itemBindOverride = { itemHolder, _, _, _ ->
                    titleDrawable =
                        if (userInfoModel.depositState == "0") R.drawable.bg_button else R.drawable.bg_white_round_blue_line
                    itemHolder.v<CommonButton>(R.id.button)
                        ?.apply {
                            setTextColor(getColor(if (userInfoModel.depositState == "0") R.color.white else R.color.theme_color))
                            text = if (userInfoModel.depositState == "0") "缴纳保证金" else "申请退回保证金"
                        }
                }

                itemClick = {
                    if (userInfoModel.depositState == "0") {
                        mViewModel.commitOrder()
                    } else {
                        if (userInfoModel.alipayNo.isNullOrEmpty()) {
                            startActivity(CompleteWithdrawActivity::class, Pair("type", 0))
                        } else {
                            navigation(
                                requireView(), R.id.action_depositFragment_to_withdrawFragment,
                                bundleOf("money" to userInfoModel.depositMoney)
                            )

                        }
                    }
                }
            }

            dslItem(ItemSingleText("保证金说明")) {
                style.titleGravity = Gravity.CENTER
                style.isBold = true
                style.topPadding = 60
                style.lineHeight = 0
            }

            dslItem(ItemSingleText(userInfoModel.depositRule)) {
                style.titleTextSize = 14f
                style.topPadding = 0
                style.leftPadding = 32
                style.rightPadding = 32
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                toPayActivity(it.money.toString(), it.id, SpKeys.PAY_TYPE_DEPOSIT)
            }, onError = { err -> toast(err.errorMsg) })
        }
        mViewModel.info.observe(this) {
            setUI(it)
        }
    }
}